package cn.yexiaofeng.bookmanager.controller;



import cn.yexiaofeng.bookmanager.beans.Admin;
import cn.yexiaofeng.bookmanager.beans.ReturnData;
import cn.yexiaofeng.bookmanager.service.AdminService;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.ServletRequest;

/**
 * 用户控制层
 */
@Controller
public class LoginController {

    @Autowired
    AdminService adminService;


    @GetMapping(value = "/toLogin")
    public  String toLogin() {
        return "login";
    }

    @GetMapping(value = "/toBack")
    public  String toBack(){
        return "back";
    }

    @PostMapping(value = "/login")
    @ResponseBody
    public  Object login(ServletRequest request) {
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        System.out.println(username+" "+password);
        Admin admin=new Admin();
        admin.setUsername(username);
        admin.setPassword(password);
        Admin admin1 = adminService.getAdmin(admin);
        ReturnData returnData=new ReturnData();
        if (admin1==null) {
            returnData.setCode(0);
        }else {
            returnData.setCode(1);
        }
        return JSON.toJSONString(returnData);
    }

}